﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CommodityItem : CommodityBase
{
    public Text m_NameT;
    public Text m_NumberT;
    public Text m_PriceT;
    public Image m_Perview;

    public Image m_LevelTagSmall;
    public Image m_LevelTagMiddle;
    public Image m_LevelTagBig;

    public Image m_DamageTypeK;
    public Image m_DamageTypeExplosion;
    public Image m_DamageTypeEnergy;
    public Callback OnBuy;

    public Item Item {
        set
        {
            if (null == value)
                return;
            m_CommodityItem = value;

            m_NameT.text = m_CommodityItem.Name;
            m_NumberT.text = m_CommodityItem.m_Number.ToString();
            m_PriceT.text = m_CommodityItem.m_Price.ToString();
            m_Perview.sprite = Resources.Load<Sprite>(m_CommodityItem.Perview);
            m_Perview.preserveAspect = true;
            if (ItemFactory.Instance.IsWeapon(m_CommodityItem.m_Type))
            {
                m_LevelTagSmall.gameObject.SetActive(false);
                m_LevelTagMiddle.gameObject.SetActive(false);
                m_LevelTagBig.gameObject.SetActive(false);

                m_DamageTypeK.gameObject.SetActive(false);
                m_DamageTypeExplosion.gameObject.SetActive(false);
                m_DamageTypeEnergy.gameObject.SetActive(false);
                Weapon weapon = m_CommodityItem as Weapon;
                switch(weapon.m_Level)
                {
                    case Weapon.Level.LEVEL_SMALL:
                        m_LevelTagSmall.gameObject.SetActive(true);
                        break;
                    case Weapon.Level.LEVEL_MIDDLE:
                        m_LevelTagMiddle.gameObject.SetActive(true);
                        break;
                    case Weapon.Level.LEVEL_BIG:
                        m_LevelTagBig.gameObject.SetActive(true);
                        break;
                }
                switch (weapon.m_DamageType)
                {
                    case Weapon.DamageType.TYPE_KINETIC:
                        m_DamageTypeK.gameObject.SetActive(true);
                        break;
                    case Weapon.DamageType.TYPE_ENERGY:
                        m_DamageTypeEnergy.gameObject.SetActive(true);
                        break;
                    case Weapon.DamageType.TYPE_EXPLOSION:
                        m_DamageTypeExplosion.gameObject.SetActive(true);
                        break;
                }
            }
        }
        get { return m_CommodityItem; }
    }
    Item m_CommodityItem;
    public override void onBuyClicked()
    {
        if (Player.Instance.Money < m_CommodityItem.m_Price)
        {
            MessageShow.Instance.Messaage("你钱不够");
            return;
        }
        if (m_CommodityItem.m_Number <= 0)
        {
            MessageShow.Instance.Messaage("已经一点都没有了！");
            return;
        }
        Player.Instance.Money -= m_CommodityItem.m_Price;
        Player.Instance.addItem(m_CommodityItem.m_Type, 1);
        MessageShow.Instance.Messaage("已将\""+ m_CommodityItem .Name+ "\"放入仓库");

        m_CommodityItem.m_Number--;
        m_NumberT.text = m_CommodityItem.m_Number.ToString();
        Player.Instance.ShopBuyItem(m_CommodityItem.m_Number * 1000 + m_CommodityID);
        if (m_CommodityItem.m_Number <= 0)
        {
            gameObject.SetActive(false);
        }
        if(OnBuy != null)
            OnBuy();
    }

    float m_DownTimeCount = 0;
    bool m_IsDown = false;
    override protected void Update()
    {
        base.Update();
        if(m_IsDown)
            m_DownTimeCount += Time.deltaTime;
        else
            m_DownTimeCount = 0;

        if(m_DownTimeCount > 0.7f)
        {
            onBuyClicked();
            m_DownTimeCount -= 0.1f;
        }
    }
    protected override void showDescritionPanel()
    {
        base.showDescritionPanel();
        WeaponDescriptionPanel panel = m_DescriptionPanel.GetComponent<WeaponDescriptionPanel>();
        panel.PerviewWeapon = m_CommodityItem as Weapon;
    }

    public void OnDown()
    {
        m_IsDown = true;
    }
    public void OnUp()
    {
        m_IsDown = false;
    }
    public void OnEnter()
    {
        hideDescriptionPanel();
        m_IsHover = false;
    }
    public void OnExit()
    {
        m_IsHover = true;
    }
}
